package com.efast.cafe.framework.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.portal.bean.common.PortalUserSecurityBean;
import com.efast.cafe.util.StringUtil;
@Component
public class ReqHeaderAndResHeaderInterceptor extends HandlerInterceptorAdapter{
	/***请求header 自定义参数****/
	
	public static final String REQ_HEADER_LOCAL="local";//当前语言 当前端修改语言环境时传入
	public static final String REQ_HEADER_REQVER="reqVersion";//当前请求接口的版本
	
	/********响应header 参数**********/
	
	public static final String RES_HEADER_ENTITY_VER="entityCacheVer";//实体属性缓存版本
	public static final String RES_HEADER_LOCAL="local";//当前语言环境
	
	public static final String RES_HEADER_LOV_VER="lovCacheVer";//lov缓存版本
	
	public static final String RES_HEADER_VIEW_VER="viewCacheVer";
	
	public StringRedisTemplate template;
	
	@Autowired
	public void setTemplate(StringRedisTemplate template) {
		this.template = template;
	}




	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		
		HttpSession session = request.getSession();
		
		String loc = request.getHeader(REQ_HEADER_LOCAL);
		if(!StringUtil.isEmpty(loc)){
			session.setAttribute("request_locale", loc);
		}
		String entityVer = template.opsForValue().get(CommonConstant.SESSION_ENTITY_VER);
		if(StringUtil.isEmpty(entityVer)){
			response.setHeader(RES_HEADER_ENTITY_VER, new String("0.0.0"));
		}else{
			response.setHeader(RES_HEADER_ENTITY_VER, entityVer);
		}
		String viewCacheVer = template.opsForValue().get(CommonConstant.SESSION_VIEW_VER);
		if(StringUtil.isEmpty(viewCacheVer)){
			response.setHeader(RES_HEADER_VIEW_VER, new String("0.0.0"));
		}else{
			response.setHeader(RES_HEADER_VIEW_VER, viewCacheVer);
		}
		
		Object userobj = session.getAttribute(CommonConstant.PORTAL_USER_OBJECT);
		
		if(userobj!=null){
			PortalUserSecurityBean user = (PortalUserSecurityBean)userobj;
			HashOperations<String, String, String> ops = template.opsForHash();
			String lovVer = null;
			if(ops.hasKey(CommonConstant.SESSION_LOV_VERSION, "U_"+user.getCompany_code()+user.getUsername())){
				lovVer = ops.get(CommonConstant.SESSION_LOV_VERSION, "U_"+user.getCompany_code()+user.getUsername());
			}else if(ops.hasKey(CommonConstant.SESSION_LOV_VERSION, "E_"+user.getCompany_code())){
				lovVer = ops.get(CommonConstant.SESSION_LOV_VERSION, "E_"+user.getCompany_code());
			}else if(ops.hasKey(CommonConstant.SESSION_LOV_VERSION, "BASIC")){
				lovVer = ops.get(CommonConstant.SESSION_LOV_VERSION, "BASIC");
			}else{
				lovVer = "0.0.0";
			}
			response.setHeader(RES_HEADER_LOV_VER, lovVer);
		}
		return super.preHandle(request, response, handler);
	}
}
